#!/usr/bin/env python
import sys
#import and init pygame
import pygame
import numpy as np
from TSPClasses import TSPGame, Road

pygame.init()

game = TSPGame('easy.txt', knownMinimum=4896, startingLocation=0) #manually add first location to indicate start
game.setSolution('easy-sol.txt')
#game.setDims(1270,1000)
#game.setPixToMConversion(0.634782609)
#game.setBackground('map.png')


clock = pygame.time.Clock()
game.draw()

while True:
  clock.tick(100) #allow other processes to work...
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      sys.exit(0)
    else:
      #print event
      if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: #1==Left
        #draw a line - see http://www.pygame.org/docs/ref/draw.html for more 
        x,y = event.pos
        #print x,',',y
        #check if mouse click is close to a locations
        found = game.selectCity(np.array([x,y]))
        if found != None:
          #print "closest location at %s"%found.getCoords()
          if found.getStatus(): #last marked location: remove road and unmark
            game.trip.pop()
            if game.trip.size() != 0: #if empty, don't do anything
              game.roads.pop()
              game.trip.Last().toggleStatus()
            found.toggleStatus()
          else:
            if not game.trip.has(found): #if location is not already in trip
              if game.trip.size() != 0:#not found.visited(): #new location
                game.roads.push(Road(game.trip.Last(), found))
                game.trip.Last().toggleStatus() #set prior location not active
              game.trip.push(found)
              found.toggleStatus()
            if (game.trip.size() == game.locations.size()) and found == game.trip.First(): #all locations clicked and currently on first loc
              game.roads.push(Road(game.trip.Last(), found))
              game.trip.Last().toggleStatus() #set prior location not active
              game.trip.push(found)
              found.toggleStatus()
          game.draw()
        
      #show solution
      if event.type == pygame.KEYUP and event.key == pygame.K_s:
        #print 's pressed'
        game.toggleVisibleSolution()
        game.draw()
      #clear screen and restart
      if event.type == pygame.KEYUP and event.key == pygame.K_c:
        #clear
        sol=game.getSolutionFile()
        startLoc = game.startingLocation
        game = TSPGame(game.getLocationFile(), knownMinimum=game.getKnownMinimum(), startingLocation=startLoc)
        if sol != '': game.setSolution(sol)
        game.draw()
      #load easy problem
      if event.type == pygame.KEYUP and event.key == pygame.K_1:
        #load easy game
        game = TSPGame('easy.txt', knownMinimum=4896, startingLocation=0) #manually add first location to indicate start
        game.setSolution('easy-sol.txt')
        game.draw()
      #load hard problem
      if event.type == pygame.KEYUP and event.key == pygame.K_2:
        #load hard game
        game = TSPGame('hard.txt', knownMinimum=8753)
        game.setSolution('hard-sol.txt')
        game.draw()
    #draw it to the screen
    pygame.display.flip()
